#pragma once

#include <iostream>
#include <memory>

#include <queue>
#include <vector>

#include "Mutex.hpp"
#include "Log.hpp"
#include "Thread.hpp"
#include "Cond.hpp"

namespace ThreadPoolModule
{
    using namespace ThreadModule;
    using namespace LogModule;
    using namespace LockModule;
    using namespace CondModule;

    template <class T>
    class ThreadPool
    {
    private:
        bool task_empty() { return _tasks.empty(); }
        void Handler_task(std::string name)
        {
            LOG(LogLevel::INFO) << "线程: " << name << ", 进入HandlerTask的逻辑";
            while (true)
            {
                T task;
                {
                    LockGuard lock(_mutex_lock);
                    if (_isrunning && task_empty())
                    {
                        _nr_wait--;
                        _cond.Wait(_mutex_lock);
                        _nr_wait++;
                    }

                    if (!_isrunning && task_empty())
                    {
                        break;
                    }

                    task = _tasks.front();
                    _tasks.pop();
                } // LockGuard destructed
                task(name);
            }
            LOG(LogLevel::INFO) << "线程: " << name << " 退出";
        }

    public:
        ThreadPool(int nr_thread = _default_thread_num)
            : _nr_wait(0), _isrunning(false)
        {
            for (int i = 0; i < nr_thread; ++i)
            {
                // 对于成员函数的引用必须加上&，其他函数指针不需要
                _threads.push_back(std::make_shared<Thread>(std::bind(&ThreadPool::Handler_task, this, std::placeholders::_1)));
            }
        }
        void Enqueue(T &&task)
        {
            LockGuard lock(_mutex_lock);
            if(!_isrunning) return;
            _tasks.push(move(task));
            if (_nr_wait)
            {
                _cond.Notify();
            }
        }

        // create thread
        void Start()
        {
            LockGuard lock(_mutex_lock);
            if (!_isrunning)
            {
                for (std::shared_ptr<Thread> &e : _threads)
                {
                    e->Start();
                }
                _isrunning = true;
            }
        }

        void Stop()
        {
            LockGuard lock(_mutex_lock);
            if (_isrunning)
            {
                _isrunning = false;
                if(_nr_wait)
                    _cond.NotifyAll();
            }
        }

        void End()
        {
            for (std::shared_ptr<Thread> &e : _threads)
            {
                e->Join();
                LOG(LogLevel::INFO) << "Collect thread: " << e->Name() << " success!";
            }
        }

        ~ThreadPool() {}

    private:
        std::vector<std::shared_ptr<Thread>> _threads;
        std::queue<T> _tasks;

        Mutex _mutex_lock;

        Cond _cond;
        int _nr_wait;

        bool _isrunning;
        inline const static int _default_thread_num = 5;

    }; // end of ThreadPool

}